package com.chookss.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.home.entity.RunningChoiceEntity;
import com.chookss.tools.Utils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;


public class BottomRunningChoiceDialog extends Dialog {
    private ListView lv;
    private LinearLayout llBg;
    private BottomListsAdapter adapter;
    private List<RunningChoiceEntity> lists = new ArrayList<>();
    private Context context;
    private SelectRunningDialogListener listener = null;

    public BottomRunningChoiceDialog(@NonNull Context context, List<RunningChoiceEntity> data, SelectRunningDialogListener listener) {
        super(context);
        this.context = context;
        this.lists = data;
        this.listener = listener;
        initLayoutRes();
    }

    public BottomRunningChoiceDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initLayoutRes();
    }

    public void setSelect(int position) {
        adapter.setSelect(position);
    }

    private void initLayoutRes() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_bottom_lists, null);
        lv = view.findViewById(R.id.lvBottom);
        llBg = view.findViewById(R.id.llBg);
        adapter = new BottomListsAdapter(context, lists);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                if (listener != null) {
                    listener.onListItemClick(position, lists);
                }
            }
        });
        setContentView(view);
        initBottom();
    }

    protected void initBottom() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        Window wm = getWindow();
        lp.width = lp.MATCH_PARENT;
        if(lists.size()>8){
            lp.height = Utils.dip2px(context,400);
        }else{
            lp.height = lp.WRAP_CONTENT;
        }
        lp.dimAmount = 0.6f;
        wm.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        wm.setGravity(Gravity.BOTTOM);
        wm.setWindowAnimations(R.style.DialogFullWidthDimNotEnabled);
        getWindow().setAttributes(lp);
    }
    class BottomListsAdapter extends BaseAdapter {
        //标题
        private List<RunningChoiceEntity> datas;
        private LayoutInflater inflater;
        private int selectPosition=-1;
        public BottomListsAdapter(Context mContext, List<RunningChoiceEntity> datas) {
            this.datas = datas;
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          BottomListsAdapter.ViewHolder holder = null;
            if (convertView == null) {
                holder = new BottomListsAdapter.ViewHolder();
                //加载
                convertView = inflater.inflate(R.layout.lv_item_bottom, parent, false);
                //绑定
                holder.lv_bottom_item_text = (TextView) convertView.findViewById(R.id.lv_bottom_item_text);
                convertView.setTag(holder);
            } else {
                holder = (BottomListsAdapter.ViewHolder) convertView.getTag();
            }
            //设置数据
            if(datas.get(position).getChoiceType()==0){
                holder.lv_bottom_item_text.setText(datas.get(position).getEmployeeName());
            }else  if(datas.get(position).getChoiceType()==1){
                holder.lv_bottom_item_text.setText(datas.get(position).getGroupName());
            }else  if(datas.get(position).getChoiceType()==2){
                holder.lv_bottom_item_text.setText(datas.get(position).getActivityName());
            }

            if(selectPosition==position){
                holder.lv_bottom_item_text.setTextColor(Color.parseColor("#E12416"));
            }else{
                holder.lv_bottom_item_text.setTextColor(Color.parseColor("#333333"));
            }
            return convertView;
        }

        public void setSelect(int position) {
            this.selectPosition = position;
            notifyDataSetChanged();
        }
        class ViewHolder {
            private TextView lv_bottom_item_text;
        }
    }
    protected void safeDismiss() {
        if (isShowing()) {
            dismiss();
        }
    }
}
